export default {
    data() {
        return {
            role_id:0,
            ruleForm: {},
            rules: {},
            mobile:'',
            username:'',
            identity:"1",
            userPassWord:'',
            trueName:''
        };
    },
    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        if(that.$route.query.role_id) {
            that.role_id = that.$route.query.role_id;
            that.detail();
        }
    },

    methods: {
        //请求api
        submitForm(formName) {
            var that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.role_id === 0) {
                        that.addRole();
                    } else {
                        that.editRole();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        /**
         * 添加员工
         * @returns {boolean}
         */
        addRole: function () {
            var that = this;
            if(!that.mobile){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入员工手机号' }`
                });
                return false;
            }
            if(!that.trueName){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入员工姓名' }`
                });
                return false;
            }
            if(!that.username){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入员工账号' }`
                });
                return false;
            }
            if(!that.userPassWord){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入密码' }`
                });
                return false;
            }

            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/api/manager/add", {
                token: that.token,
                mobile:that.mobile,
                truename:that.trueName,
                username:that.username,
                password:that.userPassWord,
                identity:that.identity,
            }, {emulateJSON: true}).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '添加成功' }`
                    });
                    that.$router.push('/set_up/user_list');
                }, function (res) {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ res.body.msg }`
                    });
                });
        },

        /**
         * 用户详情
         */
        detail:function () {
            var that = this;
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/api/manager/show_edit", {
                token: that.token,
                id:that.role_id,
            }, {emulateJSON: true}).then(
                function (res) {
                    // 处理成功的结果
                    that.mobile = res.body.mobile;
                    that.name = res.body.truename;
                    that.trueName = res.body.truename;
                    that.username = res.body.username;
                    that.identity=res.body.identity.toString();
                }, function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 返回
         */
        cancelList:function () {
            var that = this;
            that.$router.push('/set_up/user_list');
        },

        //修改
        editRole:function () {
            var that = this;
            if(!that.mobile){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入员工手机号' }`
                });
                return false;
            }
            if(!that.username){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入员工姓名' }`
                });
                return false;
            }
            if(!that.userPassWord){
                that.$message({
                    type: 'error',
                    message: `操作提示: ${ '请输入要更改的密码或者原密码' }`
                });
                return false;
            }
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/api/manager/edit", {
                token: that.token,
                id:that.role_id,
                mobile:that.mobile,
                truename:that.trueName,
                username:that.username,
                password:that.userPassWord,
                identity:that.identity,
            }, {emulateJSON: true}).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '修改成功' }`
                    });
                    that.$router.push('/set_up/user_list');
                }, function (res) {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ res.body.msg }`
                    });
                });
        }
    }
}